﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Taobao.Tools2.Domain.EntityFramework.Mappings
{
    #region DerivedModelEntityTypeConfigurationAdapter<T, TDerived>
    public class DerivedModelEntityTypeConfigurationAdapter<T, TDerived> : IDerivedEntityTypeConfigurationAdapter<T>
        where T : class
        where TDerived : class,T
    {
        public DerivedModelEntityTypeConfigurationAdapter()
        {
            HasMappedTable = false;
        }

        public IList<IDerivedModelRequireCondition<TDerived>> DerivedModelRequireConditions { get; set; }

        public bool HasMappedTable { get; protected set; }

        public System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T> BuildEntityTypeConfiguration(ModelMapping<T> modelMapping)
        {
            return modelMapping.Map<TDerived>(td =>
            {
                foreach (var condition in DerivedModelRequireConditions)
                {
                    condition.Required(td);
                }
            });
        }

    }
    #endregion

    #region SingleConditionDerivedEntityTypeConfigurationAdapter<T, TDerived>
    public class SingleConditionDerivedEntityTypeConfigurationAdapter<T, TDerived> : IDerivedEntityTypeConfigurationAdapter<T>
        where T : class
        where TDerived : class,T
    {
        public SingleConditionDerivedEntityTypeConfigurationAdapter(IDerivedModelRequireCondition<TDerived> derivedModelRequireCondition)
        {
            DerivedModelRequireCondition = derivedModelRequireCondition;
            HasMappedTable = false;
        }

        public IDerivedModelRequireCondition<TDerived> DerivedModelRequireCondition { get; set; }

        public bool HasMappedTable { get; protected set; }

        public System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T> BuildEntityTypeConfiguration(ModelMapping<T> modelMapping)
        {
            return modelMapping.Map<TDerived>(td => { DerivedModelRequireCondition.Required(td); });
        }
    }
    #endregion

    #region SinglePropertyRequiredDerivedEntityTypeConfigurationAdapter<T, TDerived, TValue>
    public class SinglePropertyRequiredDerivedEntityTypeConfigurationAdapter<T, TDerived, TValue> : SingleConditionDerivedEntityTypeConfigurationAdapter<T, TDerived>
        where T : class
        where TDerived : class,T
        where TValue : struct
    {
        public SinglePropertyRequiredDerivedEntityTypeConfigurationAdapter(string discriminator, TValue discriminatorValue)
            : base(new DerivedModelDiscriminatorRequireCondition<TDerived, TValue>(discriminator, discriminatorValue))
        {

        }
    }
    #endregion    
}
